package com.tensquare.article.controller;
import java.util.Map;

import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tensquare.article.model.Article;
import com.tensquare.article.service.ArticleService;

@CrossOrigin
@RestController
@RequestMapping("/article")
public class ArticleController {

	@Autowired
	private ArticleService articleService;

	private Result result = new Result(true, StatusCode.OK);


	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result delete(@PathVariable String id ){
		articleService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}


	@RequestMapping(value="/{id}",method= RequestMethod.PUT)
	public Result update(@RequestBody Article article, @PathVariable String id ){
		article.setId(id);
		articleService.update(article);
		return new Result(true,StatusCode.OK,"修改成功");
	}

	@RequestMapping(value="/examine/{id}",method= RequestMethod.PUT)
	public Result examineArticle(@PathVariable(value = "id") String articleId){
		articleService.examine(articleId);
		result.setMessage("审核成功");
		return result ;
	}


	@RequestMapping(value="/thumbup/{id}",method= RequestMethod.PUT)
	public Result addThumbupArticle(@PathVariable(value = "id") String articleId){
		articleService.addThumbup(articleId);
		result.setMessage("点赞成功");
		return result ;
	}



	@RequestMapping(method=RequestMethod.POST)
	public Result save(@RequestBody Article article  ){
		articleService.save(article);
		return new Result(true,StatusCode.OK,"增加成功");
	}


	@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
		Page<Article> pageList = articleService.findSearch(searchMap, page, size);
		return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<Article>(pageList.getTotalElements(), pageList.getContent()) );
	}


	@RequestMapping(value="/search",method = RequestMethod.POST)
	public Result findSearch( @RequestBody Map searchMap){
		return new Result(true,StatusCode.OK,"查询成功",articleService.findSearch(searchMap));
	}


	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(){
		return new Result(true, StatusCode.OK,"查询成功",articleService.findAll());
	}


	@RequestMapping(value="/{id}",method= RequestMethod.GET)
	public Result findById(@PathVariable String id){
		return new Result(true,StatusCode.OK,"查询成功",articleService.findById(id));
	}

	
}
